<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



require_once(APPPATH .'/libraries/REST_Controller.php');



/**
 * class Health
 * Will be the controller for all health actions within the vta application.
 *
 * @author      Jeroen Sen <jeroensen@22vision.nl>
 * @version     20120808
 *
 */
class Health extends REST_Controller {



    /**
     * public function all_get
     * Will act a gateway for a GET request, returns all health objects knonw in the database sorted by Name DESC.
     * curl -H "Accept: application/json" http://zichtopbomen.local/vta/condition/all
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120808
     * @param       
     * @return      void
     *
     */
    public function all_get()
    {
        $criteria = array();
        $orderBy = array('name' => 'ASC');

        $healths = $this->doctrine->em->getRepository('Entities\Health')->findBy($criteria, $orderBy);
        
        $data = array();
        
        foreach ($healths as $index => $health) {
            $data[] = array('id' => $health->getId(), 'name' => $health->getName(), 'value' => $health->getValue(), 'active' => $health->getActive());
        }
        
        $this->response(array('data' => $data));
        
    } // public function all_get()
    
    
    
    /**
     * public function create_put
     * Will add another Health to the database based on a PUT request.
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120808
     * @param       void, working of the member Array $this->put
     * @return      String true
     *
     */
    public function create_put()
    {
        $active = false;
        
        if ($this->put('active') == 'true') {
            $active = true;
        }
        
        $health = new Entities\Health;
        
        $health->setName($this->put('name'));
        $health->setValue($this->put('value'));
        $health->setActive($active);
        
        $this->doctrine->em->persist($health);
		$this->doctrine->em->flush();

        $this->response('true');
           
    } // public function create_put()
    
    
    
    /**
     * public function update_post
     * Will update a single Health object based on a POST request. Works of the member variable Array $this->post.
     * curl -X POST -H "Accept: application/json" -d '{"id":666}' http://zichtopbomen.local/vta/soil/edit
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20120808
     * @param       void
     * @return      String true
     *
     */
    public function update_post()
    {
        $id = intval($this->post('id'));
        $active = false;
        
        if ($this->post('active') == 'true') {
            $active = true;
        }
        
        
        $health = $this->doctrine->em->find('Entities\Health', $id);
                
        $health->setName($this->post('name'));
        $health->setValue($this->post('value'));
        $health->setActive($active);
        
        $this->doctrine->em->persist($health);
		$this->doctrine->em->flush();

        $this->response('true');
           
    } // public function update_post()
 
    
    
    /**
     *
     *
     */
    public function test_get()
    {
        $this->response(array('data' => 'dit is een test'));
        
    } //public function test_get()
    
    
    
    
} // class Vta extends CI_Controller